package com.atguigu.gulimall.product.controller;


import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.feign.GoService;
import com.atguigu.gulimall.product.feign.JavaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;


import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import sun.net.www.http.HttpClient;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

@RefreshScope
@RestController
@Api(tags = "TestController测试")
@RequestMapping("/")
public class IndexController {

    @Value("${user.name}")
    String name;
    @Autowired
    GoService goService;
    @Autowired
    JavaService javaService;

    @Resource
    private DiscoveryClient discoveryClient;

    @ApiOperation("测试")
    @GetMapping
    @ResponseBody
    public String index() {
        javaService.javaTest();

//        doGet("http://localhost:8080/");

        goService.goTest();
        return name;

    }

    @GetMapping("/all")
    public String all(@Param("s") String s) {
//        List<String> services = discoveryClient.getServices();
        List<ServiceInstance> instances = discoveryClient.getInstances(s);

        instances.forEach(i->{
            System.out.println(i.getScheme());
            System.out.println(i.getHost());
            System.out.println(i.getInstanceId());
            System.out.println(i.getPort());

        });
        return instances.toString();

    }

    public String doGet(String url) {
        HttpURLConnection conn = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();
        try {
            //创建远程url连接对象
            URL url2 = new URL(url);
            //通过远程url连接对象打开一个连接，强转成HTTPURLConnection类
            conn = (HttpURLConnection) url2.openConnection();
            conn.setRequestMethod("GET");
            //设置连接超时时间和读取超时时间
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(60000);
            conn.setRequestProperty("Accept", "application/json");
            //发送请求
            conn.connect();
            //通过conn取得输入流，并使用Reader读取
            if (200 == conn.getResponseCode()) {
                is = conn.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    result.append(line);
                    System.out.println(line);
                }
            } else {
                System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            conn.disconnect();
        }
        return result.toString();
    }


    @GetMapping("/test")
    public String Test() {
        return name;
    }
}
